
--[[
游戏登录界面，提供开始按钮，logo，简要设置按钮
 ]]
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

function MainScene:onCreate()
    self:addChild(cc.LayerColor:create(ColorCfg.white), 0)

    local highLevel = cc.UserDefault:getInstance():getIntegerForKey("highLevel", 1)
    if highLevel < 2 then
        -- 新游戏
        local startButton = ccui.Button:create("start.png", "start.png", "start.png")
        startButton:setPosition(display.cx, display.cy)
        startButton:setEnabled(true)
        startButton:setScale(1.5)
        startButton:addTouchEventListener(function(sender, eventType)
            print("startButton click")
            if eventType == 0 then
                sender:setScale(1.6)
            elseif eventType == 2 then
                self:getApp():enterScene("GameScene")
                sender:setScale(1.5)
            elseif eventType == 3 then
                sender:setScale(1.5)
            end
        end)
        self:addChild(startButton, 10)
    else
        -- 继续按钮
        local button1 = Util.newButton(
            "commonBtn.png",
            1,
            cc.p(display.cx, display.cy + 100),
            function()
                self:getApp():enterScene("GameScene")
            end)
        button1:setTitleText("继续第".. highLevel .. "关")
        button1:setTitleFontSize(80)
        button1:setScale9Enabled(true)
        button1:setContentSize(display.width * 0.8, 150)
        self:addChild(button1, 10)
        -- 重玩按钮
        local button1 = Util.newButton(
            "commonBtn.png",
            1,
            cc.p(display.cx, display.cy - 150),
            function()
                cc.UserDefault:getInstance():setIntegerForKey("highLevel", 1)
                cc.UserDefault:getInstance():setIntegerForKey("highLevelScore", 0)
                cc.UserDefault:getInstance():flush()
                self:getApp():enterScene("GameScene")
            end)
        button1:setTitleText("重新开始")
        button1:setTitleFontSize(80)
        button1:setScale9Enabled(true)
        button1:setContentSize(display.width * 0.8, 150)
        self:addChild(button1, 10)

        local tipsLabel = cc.Label:create()
        tipsLabel:setTextColor(ColorCfg.orange)
        tipsLabel:setString(GameCfg.tips[math.random(1,#GameCfg.tips)])
        tipsLabel:setSystemFontSize(60)
        tipsLabel:setDimensions(display.width-100, 0)
        tipsLabel:setHorizontalAlignment(1)
        tipsLabel:setPosition(display.cx, display.height*0.3)
        self:addChild(tipsLabel, 10)
    end

    local startLabel = cc.Label:create()
    startLabel:setTextColor(ColorCfg.orange)
    startLabel:setString("消灭星星")
    startLabel:setSystemFontSize(150)
    startLabel:setPosition(display.cx, display.height*0.8)
    self:addChild(startLabel, 10)
    cc.SimpleAudioEngine:getInstance():preloadEffect("pop_star.mp3")

    --最高分
    local highScore = cc.UserDefault:getInstance():getIntegerForKey("highScore", 0)
    print("highScore", highScore)
    if highScore > 3000 then
        -- 不到3000分的，认为玩家还不会玩，就不展示最高分了，不然展示的信息太多
        local highScoreLabel = cc.Label:create()
        highScoreLabel:setTextColor(ColorCfg.orange)
        highScoreLabel:setString("最高分：".. highScore)
        highScoreLabel:setSystemFontSize(80)
        highScoreLabel:setPosition(display.cx, 20)
        highScoreLabel:setAnchorPoint(0.5, 0)
        self:addChild(highScoreLabel, 10)
    end
    self.rankButton = Util.newButton("rank.png", 1.3, cc.p(display.width / 5 * 2.5, 190), function()
        self.highScore = cc.UserDefault:getInstance():getIntegerForKey("highScore", 0)
        local deviceId = cc.UserDefault:getInstance():getStringForKey("deviceid", "0")
        cc.Application:getInstance():openURL(GameCfg.rankUrl
                .."?imei="
                ..deviceId
                .."&score="
                ..self.highScore
        )
    end)
    self:addChild(self.rankButton, 20)

    --音乐开关
    cc.exports.MUSIC_ENABLE = cc.UserDefault:getInstance():getBoolForKey("MUSIC_ENABLE", true)
    --单双击
    cc.exports.DOUBLE_CLICK_ENABLE = cc.UserDefault:getInstance():getBoolForKey("DOUBLE_CLICK_ENABLE", false)
end

return MainScene
